package net.arvin.mobilebanksystem.fragment;

import java.util.UUID;

import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.model.CodeInfo;
import net.arvin.service.TransfersService;
import net.arvin.utils.HttpRequestClient;
import net.arvin.utils.PreferenceUtils;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.google.zxing.WriterException;
import com.zxing.encoding.EncodingHandler;

public class PayCodeFragment extends Fragment implements OnClickListener {
	private EditText money;
	private Button createBtn;
	private ImageView qrImg;

	private CodeInfo codeInfo;
	private ResponseHandler mResponseHandler;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_pay_code, null);
		init(view);
		return view;
	}

	private void init(View view) {
		codeInfo = new CodeInfo();
		initView(view);
		initListener();
	}

	private void initListener() {
		createBtn.setOnClickListener(this);
	}

	private void initView(View view) {
		money = (EditText) view.findViewById(R.id.money_et);
		createBtn = (Button) view.findViewById(R.id.create);
		qrImg = (ImageView) view.findViewById(R.id.qr_image);
	}

	// 唯一的id
	private String generateID() {
		return UUID.randomUUID().toString();
	}

	private ResponseHandler getResponseHandler(final String contentString) {
		if (mResponseHandler == null) {
			mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					Toast.makeText(getActivity(), "生成成功~", Toast.LENGTH_SHORT)
							.show();
					money.setText("");
					try {
						if (!contentString.equals("")) {
							// 根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（400*400）
							Bitmap qrCodeBitmap = EncodingHandler.createQRCode(
									contentString, 400);
							qrImg.setImageBitmap(qrCodeBitmap);
						} else {
							Toast.makeText(getActivity(), "接收金额不能为空=_=!",
									Toast.LENGTH_SHORT).show();
						}

					} catch (WriterException e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onRequestFailed(final Object data) {
					Toast.makeText(getActivity(), "生成失败=_=!",
							Toast.LENGTH_SHORT).show();
				}
			};
		}
		return mResponseHandler;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.create:
			onCreateBtnClicked();
			break;
		}
	}

	private void onCreateBtnClicked() {
		String baseUrl = HttpRequestClient.getSweepUrl();
		String id = generateID();
		String contentString = baseUrl + "?id=" + id;

		codeInfo.setId(id);
		if (money.getText().toString().equals("")
				|| money.getText().toString() == null) {
			Toast.makeText(getActivity(), "接收金额不能为空", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		codeInfo.setMoney(money.getText().toString().trim());
		codeInfo.setPhonenumber(PreferenceUtils
				.getPreferPhonenumber(getActivity()));

		String codeInfoStr = JSON.toJSONString(codeInfo);
		TransfersService.tryCreatePayCode(codeInfoStr,
				getResponseHandler(contentString), getActivity());
	}
}
